package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.Login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@TsLogin
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    /**
     * 上传声音
     *
     * @param file
     * @return
     */
    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result<Map<String, Object>> uploadTrack(MultipartFile file) {
        Map<String, Object> uploadTrackMap = trackInfoService.uploadTrack(file);
        return Result.ok(uploadTrackMap);
    }

    /**
     * 保存声音
     *
     * @param trackInfoVo
     * @return
     */
    @TsLogin
    @Operation(summary = "保存声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        long userId = AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(userId, trackInfoVo);
        return Result.ok();
    }

    /**
     * 分页查询用户声音列表
     *
     * @param pageNum
     * @param pageSize
     * @param trackInfoQuery
     * @return
     */
    @TsLogin
    @Operation(summary = "分页查询声音列表")
    @PostMapping("/findUserTrackPage/{pageNum}/{pageSize}")
    public Result<IPage<TrackListVo>> findUserTrackPage(@PathVariable Long pageNum, @PathVariable Long pageSize, @RequestBody TrackInfoQuery trackInfoQuery) {

        //  获取用户Id 封装数据
        trackInfoQuery.setUserId(AuthContextHolder.getUserId() == null ? 1L : AuthContextHolder.getUserId());
        System.out.println("trackInfoQuery = " + trackInfoQuery);

        return Result.ok(trackInfoService.findUserTrackPage(pageNum, pageSize, trackInfoQuery));
    }

    /**
     * 删除声音
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "删除声音")
    @DeleteMapping("removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Long trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    /**
     * 回显声音
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "回显声音")
    @GetMapping("getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable Long trackId) {
        TrackInfo trackInfo = trackInfoService.getById(trackId);
        return Result.ok(trackInfo);
    }

    /**
     * 修改声音
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "修改声音")
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable Long trackId, @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackId, trackInfoVo);
        return Result.ok();
    }

    /**
     * 获取专辑声音分页列表
     *
     * @param albumId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Operation(summary = "获取专辑声音分页列表")
    @TsLogin(required = false)
    @GetMapping("/findAlbumTrackPage/{albumId}/{pageNum}/{pageSize}")
    public Result findAlbumTrackPage(@PathVariable Long albumId, @PathVariable Long pageNum, @PathVariable Long pageSize) {
        //	获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //	构建分页对象
        Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pageNum, pageSize);
        //	调用服务层方法
        IPage<AlbumTrackListVo> trackPageInfo = trackInfoService.findAlbumTrackPage(albumTrackListVoPage, albumId, userId);
        //	返回数据
        return Result.ok(trackPageInfo);
    }

    /**
     * 获取用户声音分集购买支付列表
     *
     * @param trackId
     * @return
     */
    @TsLogin
    @Operation(summary = "获取用户声音分集购买支付列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
        List<Map<String, Object>> userTrackPaidList = trackInfoService.findUserTrackPaidList(trackId);
        return Result.ok(userTrackPaidList);
    }

    /**
     * 批量获取用户下单付费声音列表
     *
     * @param trackId
     * @param trackCount
     * @return
     */
    @Operation(summary = "批量获取用户下单付费声音列表")
    @GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
    Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId, @PathVariable("trackCount") Integer trackCount) {
        List<TrackInfo> userPaidTrackInfoList = trackInfoService.findPaidTrackInfoList(trackId, trackCount);
        return Result.ok(userPaidTrackInfoList);
    }

}

